package com.imooc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.imooc.enums.CommentLevel;
import com.imooc.enums.YesOrNo;
import com.imooc.mapper.*;
import com.imooc.pojo.Items;
import com.imooc.pojo.ItemsImg;
import com.imooc.pojo.ItemsParam;
import com.imooc.pojo.ItemsSpec;
import com.imooc.service.ItemService;
import com.imooc.utils.DesensitizationUtil;
import com.imooc.utils.PagedResult;
import com.imooc.vo.CommentLevelCountsVo;
import com.imooc.vo.ItemCommentVo;
import com.imooc.vo.SearchItemsVo;
import com.imooc.vo.ShopcartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

/**
 * <h1>商品服务</h1>
 *
 * @author : dzj
 * @create: 2022-02-17 17:53
 */
@Service
public class ItemServiceImpl implements ItemService {
    @Autowired
    private ItemsMapper itemsMapper;
    @Autowired
    private ItemsMapperCustom itemsMapperCustom;
    @Autowired
    private ItemsSpecMapper itemsSpecMapper;
    @Autowired
    private ItemsImgMapper itemsImgMapper;
    @Autowired
    private ItemsParamMapper itemsParamMapper;

    @Autowired
    private ItemsCommentsMapper itemsCommentsMapper;
    @Autowired
    ItemsSpecMapperCustom itemsSpecMapperCustom;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Items queryItemById(String itemId) {
        return itemsMapper.selectByPrimaryKey(itemId);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<ItemsImg> queryItemImgList(String itemId) {

        Example example = new Example(ItemsImg.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("itemId",itemId);
        return itemsImgMapper.selectByExample(example);
    }

    /**
     * 商品规格列表
     * @param itemId
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<ItemsSpec> queryItemSpecList(String itemId) {

        Example example = new Example(ItemsImg.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("itemId",itemId);
        return itemsSpecMapper.selectByExample(example);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public ItemsParam queryItemParam(String itemId) {
        Example example = new Example(ItemsImg.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("itemId",itemId);
        return itemsParamMapper.selectOneByExample(example);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public CommentLevelCountsVo queryCommentCounts(String itemId) {
        Integer good = queryCommentLevelCount(itemId, CommentLevel.GOOD.type);
        Integer normal = queryCommentLevelCount(itemId, CommentLevel.NORMAL.type);
        Integer bad = queryCommentLevelCount(itemId, CommentLevel.BAD.type);
        CommentLevelCountsVo vo = new CommentLevelCountsVo();
        vo.setBadCounts(bad);
        vo.setNormalCounts(normal);
        vo.setGoodCounts(good);
        vo.setTotalCounts(bad+normal+good);
        return vo;
    }

    /**
     *
     * @param itemId
     * @param level
     * @return
     */
    private Integer queryCommentLevelCount(String itemId,Integer level) {
       return  itemsMapperCustom.queryCommentLevelCount(itemId, level);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public PagedResult queryPagedComments(String itemId, Integer level, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Map<String,Object> map = new HashMap<>();
        map.put("itemId",itemId);
        if(level!=null) {
            map.put("level",level);
        }
        List<ItemCommentVo> list = itemsMapperCustom.queryItemComments(map);
        for (ItemCommentVo itemCommentVo : list) {
            itemCommentVo.setNickname(DesensitizationUtil.commonDisplay(itemCommentVo.getNickname()));
        }
        return setterPagedResult(list,pageNum);
    }

    /**
     * 通用分页信息
     * @param list
     * @param pageNum
     * @return
     */
    protected PagedResult setterPagedResult(List<?> list, int pageNum) {
        PageInfo<?> pageList = new PageInfo<>(list);
        PagedResult grid = new PagedResult();
        grid.setPage(pageNum);
        grid.setRows(list);
        grid.setTotal(pageList.getPages());
        grid.setRecords(pageList.getTotal());
        return grid;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public PagedResult searchItems(String keyWords, String sort, int pageNum, int pageSize) {
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("keywords",keyWords);
        paramMap.put("sort",sort);
        PageHelper.startPage(pageNum,pageSize);
        List<SearchItemsVo> list = itemsMapperCustom.searchItems(paramMap);
   /*     PageRequest of = PageRequest.of(pageNum, pageSize);
        PageImpl<SearchItemsVo> page = new PageImpl<>(list, of, list.size());
        PagedResult pagedResult = new PagedResult();
        pagedResult.setTotal(page.getTotalPages());
        pagedResult.setRecords(page.getTotalElements());
        pagedResult.setRows(list);
        pagedResult.setPage(pageNum);*/
        PagedResult pagedResult = setterPagedResult(list, pageNum);
        return pagedResult;
    }
    @Transactional(propagation = Propagation.SUPPORTS )
    @Override
    public PagedResult searchItemsByThirdCat(Integer catId, String sort, Integer page, Integer pageSize) {
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("catId",catId);
        paramMap.put("sort",sort);
        List<SearchItemsVo> list = itemsMapperCustom.searchItemsByThirdCat(paramMap);
        PageRequest of = PageRequest.of(page, pageSize);
        PageImpl<SearchItemsVo> searchItemsVos = new PageImpl<>(list, of, list.size());
        PagedResult pagedResult = new PagedResult();
        pagedResult.setPage(page);
        pagedResult.setRecords(list.size());
        pagedResult.setRows(list);
        pagedResult.setTotal(searchItemsVos.getTotalPages());
        return pagedResult;
    }

    @Override
    public List<ShopcartVo> queryItemsBySpecIds(String specIds) {
        String[] split = specIds.split(",");
        List<String> strings = Arrays.asList(split);
        List<ShopcartVo> list =  itemsMapperCustom.queryItemsBySpecIds(strings);
        return list;
    }

    @Override
    public ItemsSpec queryItemSpecById(String specId) {
        ItemsSpec itemsSpec = itemsSpecMapper.selectByPrimaryKey(specId);
        return itemsSpec;
    }

    @Override
    public String queryItemMainImgById(String itemId) {
        ItemsImg itemsImg = new ItemsImg();
        itemsImg.setItemId(itemId);
        itemsImg.setIsMain(YesOrNo.YES.getCode());
        ItemsImg result = itemsImgMapper.selectOne(itemsImg);

        return result.getUrl();
    }

    @Override
    public String getMainImgUrl(String itemId) {
        ItemsImg example = new ItemsImg();
        example.setItemId(itemId);
        example.setIsMain(YesOrNo.YES.getCode());
        ItemsImg itemsImg = itemsImgMapper.selectOne(example);
        return itemsImg.getUrl();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void decreaseItemSpecStock(String specId, int buyCounts) {
        int i = itemsSpecMapperCustom.decreaseItemSpecStock(specId,buyCounts);
        if(i==0) {
            throw new RuntimeException("商品库存不足!");
        }
    }


}
